package cn.tedu.tmall.front.mall.controller;

import cn.tedu.tmall.common.pojo.po.CurrentUser;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.mall.service.ICartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "购物车模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private ICartService cartService;

    @ApiOperation("添加购物车")
    @PostMapping("/add")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "Long"),
//            @ApiImplicitParam(name = "goodsNum", value = "商品数量", required = true, dataType = "Integer")
//    })
    public JsonResult addCart(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                              @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                              @Range(min = 1,max = 100,message = "请输入合法的商品数量") Integer goodsNum) {
        log.debug("Controller-添加购物车-用户:{},商品id:{},商品数量:{}",user,goodsId,goodsNum);
        cartService.addCart(user, goodsId, goodsNum);
        return JsonResult.ok();
    }

    /**
     * 修改商品数量-替换
     */
    @ApiOperation("修改商品数量")
    @PostMapping("/modify")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "goodsNum", value = "商品数量", required = true, dataType = "Integer")
    })
    public JsonResult modify(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                             @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                             @Range(min = 1,max = 100,message = "请输入合法的商品数量") Integer goodsNum) {
        log.debug("Controller-添加购物车-用户id:{},商品id:{}",user,goodsId);
        cartService.modify(user, goodsId, goodsNum);
        return JsonResult.ok();
    }

    /**
     * 增减商品数量-加减
     */
    @ApiOperation("增减商品数量")
    @PostMapping("/increase")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "goodsNum", value = "商品数量", required = true, dataType = "Integer")
    })
    public JsonResult increase(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                             @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                             @Range(min = -100,max = 100,message = "请输入合法的商品数量") Integer goodsNum) {
        log.debug("Controller-添加购物车-用户:{},商品id:{}",user,goodsId);
        cartService.increase(user, goodsId, goodsNum);
        return JsonResult.ok();
    }

    /**
     * 获取购物车商品列表
     */
    @ApiOperation("获取购物车商品列表")
    @PostMapping("/list")
    public JsonResult getCartList(@ApiIgnore @AuthenticationPrincipal CurrentUser user) {
        log.debug("Controller-获取购物车商品列表-用户:{}",user);
        return JsonResult.ok(cartService.getCartList(user));
    }

    /**
     * 删除购物车商品
     */
    @ApiOperation("删除购物车商品")
    @PostMapping("/delete")
    public JsonResult delete(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                             @RequestParam @Range(min = 1,message = "请输入合法的商品id") Long goodsId) {
        log.debug("Controller-删除购物车商品-用户:{},商品id:{}",user,goodsId);
        cartService.delete(user, goodsId);
        return JsonResult.ok();
    }

    //todo 选中/取消选中功能
    // 入参：user,goodsId 出参：无
    /**
     * 选中/取消选中商品
     */
    @ApiOperation("选中/取消选中商品")
    @PostMapping("/checked")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "checked", value = "是否选中", required = true, dataType = "Integer")
    })
    public JsonResult checked(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                              @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                              @Range(min = 0,max = 1,message = "请输入合法的商品id") Integer checked
                              ) {
        log.debug("Controller-选中/取消选中商品-用户:{},商品id:{},是否选中:{}",user,goodsId,checked);
        cartService.checked(user, goodsId, checked);
        return JsonResult.ok();
    }
}
